package cn.tellfish.studydemo.demo;

import java.util.Random;
import java.util.concurrent.Phaser;

public class PhserTest {


    Random rd = new Random();
    int bound = 5000;

    public void step1Task() throws InterruptedException {
        Thread.sleep(rd.nextInt(bound));
        System.out.println(Thread.currentThread().getName() + " 到达公司");
    }

    public void step2Task() throws InterruptedException {
        System.out.println("员工" + Thread.currentThread().getName() + "出发去公园");
        Thread.sleep(rd.nextInt(bound));
        System.out.println("员工" + Thread.currentThread().getName() + "到达公园");
    }

    public void step3Task() throws InterruptedException {
        System.out.println("员工" + Thread.currentThread().getName() + "出发去餐厅");
        Thread.sleep(rd.nextInt(bound));
        System.out.println("员工" + Thread.currentThread().getName() + "到达餐厅");
    }

    public void step4Task() throws InterruptedException {
        System.out.println("员工" + Thread.currentThread().getName() + "开始用餐了");
        Thread.sleep(rd.nextInt(bound));
        System.out.println("员工" + Thread.currentThread().getName() + "回家了");
    }

    public static void main(String[] args) {
        Phaser ph = new Phaser() {
            @Override
            protected boolean onAdvance(int phase, int registeredParties) {
                int staffs = registeredParties - 1;
                switch (phase) {
                    case 0:
                        System.out.println("大家都到公司了出发去公园，人数:" + staffs);
                        break;
                    case 1:
                        System.out.println("大家都到公园大门，出发去餐厅，人数:" + staffs);
                        break;
                    case 2:
                        System.out.println("大家都到餐厅了，开始用餐，人数:" + staffs);
                        break;
                }
                return registeredParties == 1;
            }
        };

        ph.register();

        PhserTest job = new PhserTest();
        for (int i = 0; i < 3; i++) {
            ph.register();
            new Thread(() -> {
                try {
                    job.step1Task();
                    ph.arriveAndAwaitAdvance();
                    job.step2Task();
                    System.out.println("员工" + Thread.currentThread().getName() + "到达公园大门");
                    ph.arriveAndAwaitAdvance();
                    job.step3Task();
                    ph.arriveAndAwaitAdvance();
                    job.step4Task();
                    ph.arriveAndDeregister();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }
        for (int i = 0; i < 2; i++) {
            ph.register();
            new Thread(() -> {
                try {
                    job.step1Task();
                    ph.arriveAndAwaitAdvance();
                    job.step2Task();
                    System.out.println("员工" + Thread.currentThread().getName() + "回家了");
                    ph.arriveAndDeregister();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }

        while (!ph.isTerminated()) {
            int phaser = ph.arriveAndAwaitAdvance();
            if (phaser == 2) {
                for (int i = 0; i < 4; i++) {
                    ph.register();
                    new Thread(() -> {
                        try {
                            job.step3Task();
                            ph.arriveAndAwaitAdvance();
                            job.step4Task();
                            ph.arriveAndDeregister();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }).start();
                }
            }
        }
    }

}
